package com.server.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.IMsgInforService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

/**
 * 消息管理
 * @author ServerZhang
 * @date 2016年8月18日
 */
@Controller
@RequestMapping("msg")
public class MsgController {
	
	private final String msgTable = "education.message";
	
	@Autowired
	private IMsgInforService msgInforService;

	 /**
	 * 消息列表页面
	 * @param request
	 * @return
	 * @author ServerZhang
	 * @date 2016年9月1日
	 */
	@RequestMapping("{isMng}")
	public ModelAndView msg(HttpServletRequest request,@PathVariable Integer isMng){
		ModelAndView mv = new ModelAndView("message/infor/list");
		if (isMng.equals(1)) {
			mv.addObject("userName",SessionUtil.getStringAttri(request, SessionData.nameKey_mng));
			mv.addObject("uuid",SessionUtil.getStringAttri(request, SessionData.uuidKey_mng));
		}else {
			mv.addObject("userName",SessionUtil.getStringAttri(request, SessionData.nameKey));
			mv.addObject("uuid",SessionUtil.getStringAttri(request, SessionData.uuidKey));
		}
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	 /**
	 * 获取消息条数
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年9月1日
	 */
	@RequestMapping("count")
	public void count(HttpServletRequest request,HttpServletResponse response){
		String uuid = request.getParameter("uuid");
		String notRead = request.getParameter("notRead");
		Integer count = msgInforService.getCount(uuid, !StringUtils.isEmpty(notRead)&&"true".equals(notRead));
		OutputUtil.returnInteger(response, count);
	}
	 /**
	 * 消息列表
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年9月1日
	 */
	@RequestMapping("list")
	public void list(HttpServletRequest request,HttpServletResponse response){
		String uuid = request.getParameter("uuid");
		String notRead = request.getParameter("notRead");
		DataVector<Map<String, Object>> listData = msgInforService.getList(uuid, !StringUtils.isEmpty(notRead)&&"true".equals(notRead), new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	 /**
	 * 删除
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年9月1日
	 */
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Integer delete = msgInforService.delete(ids);
		OutputUtil.returnInteger(response, delete);
	}
	 /**
	 * 清空消息
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年9月1日
	 */
	@RequestMapping("clear")
	public void clear(HttpServletRequest request,
			HttpServletResponse response){
		String uuid = request.getParameter("uuid");
		String notRead = request.getParameter("notRead");
		Integer delete = msgInforService.deleteAll(uuid, !StringUtils.isEmpty(notRead)&&"true".equals(notRead));
		OutputUtil.returnInteger(response, delete);
	}
	 /**
	 * 设为已读
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年9月2日
	 */
	@RequestMapping("read")
	public void read(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Integer read = msgInforService.read(ids);
		OutputUtil.returnInteger(response, read);
	}
}
